﻿using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiMeiJiaService.Assembly.DB;
using SQLite;
using YiMeiJiaLibModel.Business;

namespace YiMeiJiaService.Assembly
{
    public class BaseComponent : ComponentBase
    {
        [Inject] protected NavigationManager Navigation { get; set; }

        protected override async Task OnInitializedAsync()
        {
            await CheckLoginAndNavigate();
            await base.OnInitializedAsync();
        }

        /// <summary>
        /// 检查登录
        /// 如果过期 或者没有登录的话，就返回登录界面
        /// </summary>
        /// <returns></returns>
        private async Task CheckLoginAndNavigate()
        {

            var url = Navigation.ToBaseRelativePath(Navigation.BaseUri);
            if (!(url.ToLower().Contains("/account/login")))
            {
                var db =  await YMJLifeSqlite.Db();
                var count =await  db.Table<AdnroidTokens>().FirstOrDefaultAsync();
                if(count == null) {
                    Navigation.NavigateTo("/Account/Login");
                    return;
                }
                if(count.EffecTime<DateTime.Now)
                {
                    Navigation.NavigateTo("/Account/Login");
                    return;
                }
            }
 
        }
    }
}
